//
//  ProjectDetailViewController.swift
//  cxd4iphone
//
//  Created by hexy on 12/3/15.
//  Copyright © 2015 hexy. All rights reserved.
//

import UIKit

let bidDetailHeaderViewReusedId = "bidDetailHeaderViewReusedId"
class BidDetailViewController: BaseTableViewController {

    private lazy var bidDetailListViewModel = BidDetailListViewModel()
    
    var bidViewModel = BidViewModel(bidModel: BidModel(dict: ["":""]))
    
    init(bidViewModel: BidViewModel) {
        
        super.init(style: .Plain)
        self.title = "项目详情"
        self.bidViewModel = bidViewModel
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
       loadData()
    }

    private func loadData() {
        
        for bidDescribeModel in bidViewModel.bidModel.listLoanInfoList! {
            
            bidDetailListViewModel.sections.append([BidDetailCellViewModel(bidDescribeModel: bidDescribeModel)])
        }
        
        self.tableView.reloadData()
    }
    
    override func makeTableView() {
        
        
        self.tableListViewModel = bidDetailListViewModel
        
        tableView.registerClass(BidDetailListCell.self, forCellReuseIdentifier: BidDetailListCellReuseID)
        
        tableView.registerClass(UITableViewHeaderFooterView.self, forHeaderFooterViewReuseIdentifier: bidDetailHeaderViewReusedId)
        
        let rect = CGRectMake(0, 0, xx_screenWidth(), 200)
        let footer = UIView(frame: rect)
        footer.backgroundColor = xx_randomColor()
        
        tableView.tableFooterView = footer
        
        tableView.sectionHeaderHeight = xx_height(30)
    }
    
}

extension BidDetailViewController {
    
    
    override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        let vm = bidDetailListViewModel.sections[section][0] as! BidDetailCellViewModel
        
        let view = tableView.dequeueReusableHeaderFooterViewWithIdentifier(bidDetailHeaderViewReusedId)
        
        view!.textLabel?.text = vm.bidDescribeModel.key
        return view
    }
    
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        let viewModel = bidDetailListViewModel.sections[indexPath.section][indexPath.row] as! BidDetailCellViewModel
        
        if viewModel.rowHeight > 0 {
            return viewModel.rowHeight
        }
        
        let cell = tableView.dequeueReusableCellWithIdentifier(viewModel.reuseId) as! BidDetailListCell
        
        viewModel.rowHeight = cell.rowHeight(viewModel)
        
        return viewModel.rowHeight
    }

}
